public class TwoDParityCheck {
	
	public static int[] check(String input){
		//Adds 8 bits for the column check, and an extra bit space for every row for row check
		int[] bits = new int[input.length() + (input.length() / 7) + 8];
		int bitspos = 0;
		int bytestart = 0;
		int inputpos = 0;
		
		/*Copy bits from String into array*/
		//Iterates through bytes
		while(bytestart <= bits.length - 16){
			//While bit selected is in the byte
			for(int i = 0; i < 7; i++){
				if(input.charAt(inputpos) == '1'){
					bits[bitspos] = 1;
				}
				else{
					bits[bitspos] = 0;
				}
				bitspos++;
				inputpos++;
			}
			//Program gets to here when 7 bits have been entered into a row
			//Adds row parity to end of byte
			bits[bitspos]= getRowParity(bits,bytestart);
			bitspos++;
			bytestart = bitspos;
		}
		/*Insert Column Parities byte to end of transmission*/
		for(int colnum = 0;colnum<=7;colnum++){
			bits[bitspos] = getColumnParity(bits,colnum);
			bitspos++;
		}
		return bits;
		
	}
	
	//Return the parity for the row
	private static int getRowParity(int[] bits,int bytestart){
		int bitcount = 0;
		for(int i = bytestart;i < bytestart+7;i++){
			if(bits[i] == 1){
				bitcount++;
			}
		}
		//If amt of set bits is even
		if(bitcount % 2 == 0){
			return 0;
		}
		else{
			return 1;
		}
	}
	
	//Return the parity for the column
	private static int getColumnParity(int[] bits,int colnum){
		int bitcount = 0;
		for(int row = 0;row <= (bits.length - 8) / 8; row++){
			if(bits[colnum + row*8] == 1){
				bitcount++;
			}
		}
		//If amt of set bits is even
		if(bitcount % 2 == 0){
			return 0;
		}
		else{
			return 1;
		}
	}
}
